var Application = function () {

    var basePath = ROOT_PATH + '/upms/manage/',
        pagePath = ROOT_PATH + 'pages/application/',
        $dialog = null,
        $dataGrid = null;

    function initGrid(gridId) {
        var gid = gridId || '#grid-data';
        $dataGrid = BsGridUtil.initBsGrid({
            gridId: gid,
            url: basePath + "applist",
            toolbar:"#appTb",
            singleSelect:false,
            rowStyle: function (value, row, index) {
                if (value.status && value.status.code == 2) {
                    return {css: {"color": "red"}}
                } else {
                    return value;
                }
            },
            columns: [
                {field: 'selectItem', checkbox:true},
                {field: 'id', title: 'Id',sortable: true},
                {field: 'name', title: '系统名称'},
                {field: 'ip', title: 'ip地址'},
                {field: 'port', title: '端口'},
                {field: 'status', title: '系统状态',formatter: FormUtils.enumFormatter},
                {field: 'updateTime', title: '修改时间'},
                {field: 'createTime', title: '创建时间'},
                {
                    field: 'optFormatter', title: '操作',
                    formatter: function (column, row) {
                        return '<a href="javascript:Application.assignSystemResource(' + row.id + ')" class="btn btn-xs btn-primary"><span class="fa fa-hand-o-right"></span>分配资源</a>';
                    }
                }
            ],
        });
        return $dataGrid;
    }

    /**
     * 分配系统资源
     */
    function assignSystemResource() {
        $.Get(pagePath + "application-assignSource.html",function (page) {
            $dialog = $(page);
            var $form = $dialog.find("form:first");
            $dialog.modal().on('shown.bs.modal', function (e) {
                BsGridUtil.initBsTreeGrid({
                    gridId: $form.find("#systemGrid"),
                    url: ROOT_PATH + "upms/resource/list",
                    toolbar:"",
                    singleSelect:false,
                    treeColumn:1,
                    columns: [
                        {field: 'btSelectItem', checkbox:true},
                        {field: 'id', title:"id",visible:false},
                        {field: 'name', title:"名称",width:270},
                        {field: 'icon', title:"图标",formatter:function (val) {
                            if (val) {
                                return "<span class='"+ val +"'></span>"
                            }
                        }},
                        {field: 'seq', title:"排序"},
                        {field: 'type', title:"资源类型",formatter:function(val){
                            if (val && val == 1){
                                return "菜单";
                            }else{
                                return "资源";
                            }
                        }},
                        {field: 'url', title:"路径",},
                        {field: 'status', title:"状态",formatter:FormUtils.enumFormatter,cellStyle:function(value,row,index){
                            if (value && value.code == 2){
                                return {css:{"color":"red"}}
                            }else{
                                return {css:{"color":"blue"}}
                            }
                        }},
                    ],
                    onCheck: function (row) {

                    },
                });
            })
        })
    }

    return {
        initGrid: initGrid,
        assignSystemResource:assignSystemResource
    }
}();